package com.haizhi.factory;

import com.haizhi.mongo.MongoHandler;
import com.haizhi.util.ConfUtil;
import com.mongodb.client.MongoDatabase;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: youfeng
 * Date: 2018-06-25
 * Time: 19:25
 */
public class MongodbFactory {

    private static final Logger logger = LogManager.getLogger(MongodbFactory.class);

    public static MongoDatabase get(String path) throws Exception {
        Properties properties = ConfUtil.getProperties(path);
        if (null == properties) {
            throw new Exception("初始化mongo配置文件失败");
        }
        MongoHandler mongoHandler = new MongoHandler(properties.getProperty("mongo.host"),
                properties.getProperty("mongo.username"),
                properties.getProperty("mongo.password"),
                properties.getProperty("mongo.auth.db"));

        //获得mongodb表操作
        MongoDatabase mongoDatabase = mongoHandler.getDb(properties.getProperty("mongo.database"));

        logger.info("mongodb初始化完成...");
        return mongoDatabase;
    }
}
